#include "imageprovider.h"
#include <QDebug>

ImageProvider::ImageProvider(): QQuickImageProvider(QQuickImageProvider::Image)
{

}

ImageProvider* ImageProvider::GetInstance()
{
    static ImageProvider single;
    return &single;
}

ImageProvider::~ImageProvider(){

}

QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
    auto pic = m_MapImages.find(id);
    QImage img;
    if(m_MapImages.end() != pic){
        img = pic.value();
    }
    return img;
}

//QPixmap ImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
//{
//    auto pic = m_MapImages.find(id);
//    QImage img;
//    if(m_MapImages.end() != pic){
//        img = pic.value();
//    }
//    return QPixmap::fromImage(img);
//}

void ImageProvider::setImage(const QString &id, const QImage &image){
    if(m_MapImages.contains(id)){
        m_MapImages[id] = image;
    }else{
        m_MapImages.insert(id,image);
    }
}

QImage* ImageProvider::findImage(const QString &fileName){
    if(m_MapImages.contains(fileName)){
       return (QImage*)(&m_MapImages[fileName]);
    }
    return nullptr;
}

/*********************************************/

ShowImage::ShowImage(QObject *parent):QObject(parent)
{

}

ShowImage::~ShowImage(){
}

void ShowImage::doAddImage(const QString &id, const QImage &image)
{
    ImageProvider::GetInstance()->setImage(id,image);
    emit callQmlRefeshImg();
}

ImageProvider* ShowImage::getInstance(){
    return ImageProvider::GetInstance();
}
